package com.xyzwps.paimon.furina.modules.userinfo.entity

import com.xyzwps.paimon.furina.common.DateTimeUtil
import com.xyzwps.paimon.furina.common.value.AvatarUrl
import com.xyzwps.paimon.furina.common.value.Nickname
import com.xyzwps.paimon.furina.common.value.UserDescription
import kotlinx.datetime.LocalDateTime
import org.jetbrains.exposed.sql.Column
import org.jetbrains.exposed.sql.ResultRow
import org.jetbrains.exposed.sql.Table
import org.jetbrains.exposed.sql.kotlin.datetime.datetime

data class UserInfo(
    val userId: Long,
    val nickname: Nickname,
    val avatar: AvatarUrl,
    val description: UserDescription,
    val createTime: LocalDateTime,
    val updateTime: LocalDateTime
) {

    constructor(row: ResultRow) : this(
        userId = row[TABLE.userId],
        nickname = Nickname(row[TABLE.nickname]),
        avatar = AvatarUrl(row[TABLE.avatar]),
        description = UserDescription(row[TABLE.description]),
        createTime = row[TABLE.createTime],
        updateTime = row[TABLE.updateTime],
    )

    companion object {
        object TABLE : Table("user_info") {
            val userId: Column<Long> = long("user_id")
            val nickname: Column<String> = varchar("nickname", Nickname.MAX_LENGTH)
            val avatar: Column<String> = varchar("avatar", AvatarUrl.MAX_LENGTH)
            val description: Column<String> = varchar("description", UserDescription.MAX_LENGTH)
            val createTime: Column<LocalDateTime> = datetime("create_time")
            val updateTime: Column<LocalDateTime> = datetime("update_time")

            override val primaryKey = PrimaryKey(userId)
        }

        fun empty(userId: Long) = UserInfo(
            userId = userId,
            nickname = Nickname.EMPTY,
            avatar = AvatarUrl.EMPTY,
            description = UserDescription.EMPTY,
            createTime = DateTimeUtil.currentLocalDateTime(),
            updateTime = DateTimeUtil.currentLocalDateTime(),
        )
    }


}
